<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="供应商编号" prop="supplierNo">
        <el-input
          v-model="queryParams.supplierNo"
          placeholder="请输入供应商编号"
          clearabledev
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商简写" prop="nickname">
        <el-input
            v-model="queryParams.nickname"
            placeholder="请输入供应商简写"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商全称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入供应商全称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <splitpanes :horizontal="false" class="default-theme">
      <!--供应商数据-->
      <pane size="60">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['scm:supplier:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['scm:supplier:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['scm:supplier:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['scm:supplier:export']">导出
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Upload" @click="handleImport" v-hasPermi="['system:supplier:import']">导入</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange" ref="supplierTable" highlight-current-row @current-change="handleCurrentSupplier">
          <el-table-column type="selection" width="55" align="center" />
<!--          <el-table-column label="ID" align="center" prop="id" />-->
          <el-table-column label="编号" align="left" prop="supplierNo" width="90"/>
          <el-table-column label="供应商简写" align="left" prop="nickname" width="90"/>
          <el-table-column label="供应商全称" align="left" prop="name" width="150" />
<!--          <el-table-column label=" 客户标签、商品" align="center" prop="tags" />-->
<!--          <el-table-column label="手机" align="center" prop="mobile" />-->
<!--          <el-table-column label="电话" align="center" prop="telephone" />-->
<!--          <el-table-column label="成交状态0未成交1成交" align="center" prop="dealStatus" />-->
<!--          <el-table-column label="首次成交时间" align="center" prop="firstDealTime" width="180">-->
<!--            <template #default="scope">-->
<!--              <span>{{ parseTime(scope.row.firstDealTime, '{y}-{m}-{d}') }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="地址" align="left" prop="address" />
<!--          <el-table-column label="区分" align="center" prop="area" />-->
<!--          <el-table-column label="负责人ID" align="center" prop="ownerUserId" />-->
<!--          <el-table-column label="负责人姓名" align="center" prop="ownerUserName" />-->
<!--          <el-table-column label="备注" align="center" prop="remark" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:supplier:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:supplier:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />

      </pane>
      <pane size="40">
        <el-tabs type="border-card" style="height: 100vh;">
          <el-tab-pane label="供应商信息">
            <el-form :model="paneForm" inline label-width="85px" :disabled=true>
              <el-form-item label="客户名称" prop="name" style="width:100%">
                <el-input v-model="paneForm.name" placeholder="请选择客户" />
              </el-form-item>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="负责人" prop="legalPerson" style="width:100%">
                    <el-input v-model="paneForm.legalPerson" placeholder="-" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话" prop="telephone" style="width:100%">
                    <el-input v-model="paneForm.telephone" placeholder="-" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="24">
                  <el-form-item label="电子邮箱" prop="email" style="width: 100%">
                    <el-input v-model="paneForm.email" placeholder="请输入电子邮箱"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="12">
                  <el-form-item label="国内外客户" prop="foreignInd" style="width: 100%">
                    <el-select v-model="paneForm.foreignInd" placeholder="请选择是否国内外客户">
                      <el-option
                          v-for="dict in scm_foreign_ind"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="所属国家" prop="country" style="width: 100%">
                    <el-input v-model="paneForm.country" placeholder="请输入所属国家" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="地址" prop="address" label-width="130px" style="width:100%">
                <el-input v-model="paneForm.address" placeholder="-" />
              </el-form-item>
              <el-form-item label="统一社会信用代码" prop="registerNo" label-width="130px" style="width:100%">
                <el-input v-model="paneForm.registerNo" placeholder="-" />
              </el-form-item>
              <el-form-item label="开户银行" prop="bankName" label-width="130px" style="width:100%">
                <el-input v-model="paneForm.bankName" placeholder="-" />
              </el-form-item>
              <el-form-item label="银行账户" prop="bankAccount" label-width="130px" style="width:100%">
                <el-input v-model="paneForm.bankAccount" placeholder="-" />
              </el-form-item>
              <el-form-item label="备注" prop="remark" style="width:100%">
                <el-input v-model="paneForm.remark" type="textarea" placeholder="-" :rows="4" />
              </el-form-item>
              <el-divider content-position="center"> </el-divider>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="业务担当" prop="ownerUserName" style="width:100%">
                    <el-input v-model="paneForm.ownerUserName" placeholder="-" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="联系人">
            <el-table v-loading="paneLoading" :data="paneSupplierContactsList" @selection-change="handleSelectionChange" :scrollbar-always-on="true" >
              <el-table-column label="姓名" align="center" prop="name" width="120"/>
              <el-table-column label="手机" align="center" prop="mobile" width="120"/>
              <el-table-column label="电子邮箱" align="center" prop="email" width="120"/>
              <el-table-column label="微信号" align="center" prop="wechat" />
              <el-table-column label="决策人" align="center" prop="decisionInd">
                <template #default="scope">
                  <dict-tag :options="scm_decision_ind" :value="scope.row.decisionInd"/>
                </template>
              </el-table-column>
              <el-table-column label="职务" align="center" prop="post" />
              <el-table-column label="性别" align="center" prop="sex">
                <template #default="scope">
                  <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
                </template>
              </el-table-column>
              <el-table-column label="负责人" align="center" prop="ownerUserName" />
              <el-table-column label="备注" align="center" prop="remark" />
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="收货地址">
            <el-table v-loading="paneLoading" :data="supplierAddressList" @selection-change="handleSelectionChange">
              <!--              <el-table-column type="selection" width="55" align="center" />-->
              <!--              <el-table-column label="ID" align="center" prop="id" />-->
              <!--              <el-table-column label="客户ID" align="center" prop="supplierId" />-->
              <el-table-column label="邮编" align="center" prop="zipCode" />
              <el-table-column label="电话" align="center" prop="telephone" />
              <el-table-column label="地址" align="center" prop="detailAddress" />
              <el-table-column label="备注" align="center" prop="remark" />
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button link type="primary" icon="Edit" @click="handleUpdateAddress(scope.row)" v-hasPermi="['scm:supplierAddress:edit']"></el-button>
                  <el-button link type="primary" icon="Delete" @click="handleDeleteAddress(scope.row)" v-hasPermi="['scm:supplierAddress:remove']"></el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAddAddress"
                v-hasPermi="['scm:supplierAddress:add']"
            >新增地址</el-button>
          </el-tab-pane>
        </el-tabs>
      </pane>
    </splitpanes>


    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" v-model="open" width="87%" append-to-body :close-on-click-modal="false">
      <el-form ref="supplierRef" :model="form" :rules="rules" label-width="85px">
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="编号" prop="supplierNo">
              <el-input v-model="form.supplierNo" placeholder="请输入供应商编号" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商简写" prop="nickname">
              <el-input v-model="form.nickname" placeholder="请输入供应商简写" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商全称" prop="name">
              <el-input v-model="form.name" placeholder="请输入供应商全称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="负责人" prop="legalPerson" style="width: 100%">
              <el-input v-model="form.legalPerson" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address" style="width:100%">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="电子邮箱" prop="email" style="width: 100%">
              <el-input v-model="form.email" placeholder="请输入电子邮箱"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="国内外客户" prop="foreignInd" style="width: 100%">
              <el-select v-model="form.foreignInd" placeholder="请选择是否国内外客户">
                <el-option
                    v-for="dict in scm_foreign_ind"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属国家" prop="country" style="width: 100%">
              <el-input v-model="form.country" placeholder="请输入业务担当" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="业务担当" prop="ownerUserName" style="width: 100%">
              <el-input v-model="form.ownerUserName" placeholder="请输入业务担当" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="统一社会信用代码" prop="bank" label-width="130px" style="width: 100%">
              <el-input v-model="form.registerNo" placeholder="请输入统一社会信用代码" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="开户银行" prop="bankName" style="width: 100%">
              <el-input v-model="form.bankName" placeholder="请输入开户银行"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="银行账户" prop="bankAccount" style="width: 100%">
              <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label=" 客户标签、商品" prop="tags">-->
<!--          <el-input v-model="form.tags" placeholder="请输入 客户标签、商品" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="手机" prop="mobile">-->
<!--          <el-input v-model="form.mobile" placeholder="请输入手机" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="首次成交时间" prop="firstDealTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.firstDealTime"-->
<!--            type="date"-->
<!--            value-format="YYYY-MM-DD"-->
<!--            placeholder="请选择首次成交时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区分" prop="area">-->
<!--          <el-input v-model="form.area" placeholder="请输入区分" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="负责人ID" prop="ownerUserId">-->
<!--          <el-input v-model="form.ownerUserId" placeholder="请输入负责人ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :rows="2"/>
        </el-form-item>
        <el-divider content-position="center">供应商联系人信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddScmSupplierContacts">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteScmSupplierContacts">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="scmSupplierContactsList" :row-class-name="rowScmSupplierContactsIndex" @selection-change="handleScmSupplierContactsSelectionChange" ref="scmSupplierContacts">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="姓名" prop="name" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入姓名" />
            </template>
          </el-table-column>
          <el-table-column label="手机" prop="mobile" width="160">
            <template #default="scope">
              <el-input v-model="scope.row.mobile" placeholder="请输入手机" />
            </template>
          </el-table-column>
<!--          <el-table-column label="生日" prop="birthday" width="240">-->
<!--            <template #default="scope">-->
<!--              <el-date-picker clearable-->
<!--                v-model="scope.row.birthday"-->
<!--                type="date"-->
<!--                value-format="YYYY-MM-DD"-->
<!--                placeholder="请选择生日">-->
<!--              </el-date-picker>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="电话" prop="telephone" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.telephone" placeholder="请输入电话" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="微信号" prop="wechat" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.wechat" placeholder="请输入微信号" />
            </template>
          </el-table-column>
          <el-table-column label="电子邮箱" prop="email" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.email" placeholder="请输入电子邮箱" />
            </template>
          </el-table-column>
          <el-table-column label="关键决策人" prop="decisionInd" width="100">
            <template #default="scope">
              <el-select v-model="scope.row.decisionInd" placeholder="请选择是否关键决策人" :clearable="false">
                <el-option
                    v-for="dict in scm_decision_ind"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="职务" prop="post" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.post" placeholder="请输入职务" />
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="sex" width="100">
            <template #default="scope">
              <el-select v-model="scope.row.sex" placeholder="请选择性别">
                <el-option
                    v-for="dict in sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
<!--          <el-table-column label="负责人ID" prop="ownerUserId" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.ownerUserId" placeholder="请输入负责人ID" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="负责人姓名" prop="ownerUserName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.ownerUserName" placeholder="请输入负责人姓名" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改客户地址对话框 -->
    <el-dialog :title="title" v-model="addressOpen" width="800px" append-to-body>
      <el-form ref="supplierAddressRef" :model="addressForm" :rules="rules" label-width="80px">
        <!--        <el-form-item label="客户ID" prop="supplierId">-->
        <!--          <el-input v-model="addressForm.supplierId" placeholder="请输入客户ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="addressForm.contactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="addressForm.telephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="邮编" prop="zipCode">
          <el-input v-model="addressForm.zipCode" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="地址" prop="detailAddress">
          <el-input v-model="addressForm.detailAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addressForm.remark" type="textarea" placeholder="请输入内容" :rows="4"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormAddress">确 定</el-button>
          <el-button @click="cancelAddress">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <import-excel-dialog
        v-model="showImportDialog"
        import-url="/scm/supplier/importData"
        template-url="/scm/supplier/importTemplate"
        templateFileName="供应商数据模板"
        @success="handleImportSuccess"
    />

  </div>
</template>

<script setup name="supplier">
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier } from "@/api/scm/supplier";
import { listSupplierAddress, getSupplierAddress, delSupplierAddress, addSupplierAddress, updateSupplierAddress } from "@/api/scm/supplierAddress";
import { listSupplierContacts, getSupplierContacts, delSupplierContacts, addSupplierContacts, updateSupplierContacts } from "@/api/scm/supplierContacts";
import {Pane, Splitpanes} from "splitpanes";
import "splitpanes/dist/splitpanes.css"
import ImportExcelDialog from "@/components/ExcelImport/index.vue";
// import ExcelEditor from "@/components/ExcelEditor"

const { proxy } = getCurrentInstance();
const { sys_user_sex, scm_decision_ind, scm_foreign_ind } = proxy.useDict('sys_user_sex', 'scm_decision_ind', 'scm_foreign_ind');

const supplierList = ref([]);
const scmSupplierContactsList = ref([]);
const paneSupplierContactsList = ref([]);
const supplierAddressList  = ref([]);
const open = ref(false);
const addressOpen = ref(false);
const openExcelEditor = ref(true);
const loading = ref(true);
const paneLoading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedScmSupplierContacts = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const addressTitle = ref("");
const currentRowId = ref(null);

const data = reactive({
  form: {},
  addressForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierNo: null,
    name: null,
    nickname: null,
    tags: null,
    mobile: null,
    telephone: null,
    dealStatus: null,
    firstDealTime: null,
    address: null,
    area: null,
    ownerUserId: null,
    ownerUserName: null,
  },
  rules: {
  },
  paneForm:{
    id: null,
    supplierNo: null,
    name: null,
    nickname: null,
    legalPerson: null,
    foreignInd: "N",
    country: null,
    mobile: null,
    telephone: null,
    email: null,
    address: null,
    bankName: null,
    bankAccount: null,
    ownerUserId: null,
    ownerUserName: null,
  }
});

const { queryParams, form, rules, paneForm, addressForm } = toRefs(data);

/** 查询供应商列表 */
function getList() {
  loading.value = true;
  listSupplier(queryParams.value).then(response => {
    supplierList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    // 刷新完成后尝试恢复之前的当前行
    if (currentRowId.value) {
      const foundRow = supplierList.value.find(row => row.id === currentRowId.value);
      if (foundRow) {
        nextTick(() => {
          proxy.$refs.supplierTable.setCurrentRow(foundRow); // 恢复当前行
          paneForm.value = foundRow; // 同步右侧显示
        });
      } else {
        // 如果找不到该行（比如分页后），可选择清空或设置为第一行
        if (supplierList.value.length > 0) {
          nextTick(() => {
            proxy.$refs.supplierTable.setCurrentRow(supplierList.value[0]);
            paneForm.value = supplierList.value[0];
          });
        } else {
          paneForm.value = {};
        }
      }
    } else {
      // 首次加载或未设置时默认选中第一行
      if (supplierList.value.length > 0) {
        nextTick(() => {
          proxy.$refs.supplierTable.setCurrentRow(supplierList.value[0]);
          paneForm.value = supplierList.value[0];
        });
      }
    }
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    supplierNo: null,
    name: null,
    nickname: null,
    legalPerson: null,
    foreignInd: "N",
    country: null,
    tags: null,
    mobile: null,
    telephone: null,
    email: null,
    dealStatus: null,
    firstDealTime: null,
    address: null,
    area: null,
    bankName: null,
    bankAccount: null,
    ownerUserId: null,
    ownerUserName: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  scmSupplierContactsList.value = [];
  proxy.resetForm("supplierRef");
}

// 表单重置
function addressReset() {
  addressForm.value = {
    id: null,
    supplierId: null,
    zipCode: null,
    telephone: null,
    detailAddress: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("supplierAddressRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加供应商";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSupplier(_id).then(response => {
    form.value = response.data;
    scmSupplierContactsList.value = response.data.scmSupplierContactsList;
    open.value = true;
    title.value = "修改供应商";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["supplierRef"].validate(valid => {
    if (valid) {
      form.value.scmSupplierContactsList = scmSupplierContactsList.value;
      if (form.value.id != null) {
        updateSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSupplier(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除供应商编号为"' + _ids + '"的数据项？').then(function() {
    return delSupplier(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 供应商联系人序号 */
function rowScmSupplierContactsIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 供应商联系人添加按钮操作 */
function handleAddScmSupplierContacts() {
  let obj = {};
  obj.name = "";
  obj.mobile = "";
  obj.birthday = "";
  obj.telephone = "";
  obj.wechat = "";
  obj.email = "";
  obj.decisionInd = "";
  obj.post = "";
  obj.sex = "";
  obj.ownerUserId = "";
  obj.ownerUserName = "";
  obj.remark = "";
  scmSupplierContactsList.value.push(obj);
}

/** 供应商联系人删除按钮操作 */
function handleDeleteScmSupplierContacts() {
  if (checkedScmSupplierContacts.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的供应商联系人数据");
  } else {
    const scmSupplierContactss = scmSupplierContactsList.value;
    const checkedScmSupplierContactss = checkedScmSupplierContacts.value;
    scmSupplierContactsList.value = scmSupplierContactss.filter(function(item) {
      return checkedScmSupplierContactss.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleScmSupplierContactsSelectionChange(selection) {
  checkedScmSupplierContacts.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/supplier/export', {
    ...queryParams.value
  }, `供应商信息_${new Date().getTime()}.xlsx`)
}

/** 展示用户详情 */
function handleCurrentSupplier(val){
  if (!val) return;

  currentRowId.value = val.id; // 记录当前行的 id
  paneForm.value = val;
  paneLoading.value = true;
  getContactList(val.id);
  getAddressList(val.id);
  // listSupplierContacts(queryParams.value).then(response => {
  //  paneSupplierContactsList.value = response.rows;
  //   total.value = response.total;
  //   paneLoading.value = false;
  // });

}
function handleSelectUser(row) {
  paneForm.value = row;
  // queryParamsUser.value.tenantId = row.id;
  // currentTenant.value = {
  //   id: row.id,
  //   name: row.name,
  //   contactName: row.contactName,
  //   contactMobile: row.contactMobile,
  //   packageId: row.packageId
  // };
}
/** 查询联系人列表 */
function getContactList(supplierId) {
  paneLoading.value = true;
  const contactQueryParams = ref({
    pageNum: 1,
    pageSize: 100,
    supplierId: supplierId,
  });
  listSupplierContacts(contactQueryParams.value).then(response => {
    paneSupplierContactsList.value = response.rows;
    paneLoading.value = false;
  });
}
/** 查询客户地址列表 */
function getAddressList(supplierId) {
  paneLoading.value = true;
  const addressQueryParams = ref({
    pageNum: 1,
    pageSize: 20,
    supplierId: supplierId,
  });
  listSupplierAddress(addressQueryParams.value).then(response => {
    supplierAddressList.value = response.rows;
    paneLoading.value = false;
  });
}

// 取消按钮
function cancelAddress() {
  addressOpen.value = false;
  reset();
}
/** 新增按钮操作 */
function handleAddAddress() {
  if (!currentRowId.value){
    proxy.$modal.msgError("请先选择客户");
    return;
  }
  addressReset();
  addressForm.value.supplierId = currentRowId.value;
  addressOpen.value = true;
  addressTitle.value = "添加客户地址";

}
/** 修改按钮操作 */
function handleUpdateAddress(row) {
  addressReset();
  const _id = row.id || ids.value
  getSupplierAddress(_id).then(response => {
    addressForm.value = response.data;
    addressOpen.value = true;
    addressTitle.value = "修改客户地址";
  });
}

/** 提交按钮 */
function submitFormAddress() {
  proxy.$refs["supplierAddressRef"].validate(valid => {
    if (valid) {
      if (addressForm.value.id != null) {
        updateSupplierAddress(addressForm.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          addressOpen.value = false;
          if (currentRowId.value) {
            getAddressList(currentRowId.value);
          }
        });
      } else {
        addSupplierAddress(addressForm.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          addressOpen.value = false;
          if (currentRowId.value) {
            getAddressList(currentRowId.value);
          }
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDeleteAddress(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户地址编号为"' + _ids + '"的数据项？').then(function() {
    return delSupplierAddress(_ids);
  }).then(() => {
    if (currentRowId.value) {
      getAddressList(currentRowId.value);
    }
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导入  **/
const showImportDialog = ref(false)
/** 导入按钮操作 */
function handleImport() {
  // upload.title = "用户导入";
  showImportDialog.value = true;
}
function handleImportSuccess(response) {
  console.log('导入成功:', response)
  getList();
}

getList();
</script>
